home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapter4 / tabprivs.c < prev    next >
C/C++ Source or Header  |  1996-03-21  |  10KB  |  279 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "tabprivs.h"
  5. #include "sqlext.h"  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "SQLTablePrivileges()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static HENV  hEnv  = NULL;
  110. static HDBC  hDBC  = NULL;
  111. static HWND  hList = NULL;
  112.  
  113.    switch( uMsg )
  114.    {
  115.       case WM_CREATE :
  116.               {
  117.                  RETCODE retCode;
  118.  
  119.                  // Allocate Environment and Connection.
  120.                  //.....................................
  121.                  SQLAllocEnv( &hEnv );
  122.                  SQLAllocConnect( hEnv, &hDBC );
  123.  
  124.                  // Connect to the data source
  125.                  //...........................
  126.                  retCode = SQLConnect( hDBC, 
  127.                              "Test Data Source", SQL_NTS,
  128.                              "DBA",              SQL_NTS,  
  129.                              "SQL",              SQL_NTS );
  130.  
  131.                  if ( retCode == SQL_SUCCESS )
  132.                  {
  133.                     int nTabSize = 60;
  134.  
  135.                     hList = CreateWindow( "LISTBOX", "",    
  136.                                LBS_NOTIFY | LBS_USETABSTOPS |
  137.                                LBS_NOINTEGRALHEIGHT  | LBS_SORT | 
  138.                                WS_BORDER  | WS_CHILD | 
  139.                                WS_VISIBLE | WS_VSCROLL, 
  140.                                0, 0, 0, 0, 
  141.                                hWnd, (HMENU)101,
  142.                                hInst, NULL );
  143.                     
  144.                     SendMessage( hList, LB_SETTABSTOPS, 
  145.                                  1, (LPARAM)&nTabSize );
  146.                  }
  147.               }
  148.               break;
  149.  
  150.       case WM_SIZE :
  151.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  152.                                        HIWORD( lParam ), TRUE );
  153.               break; 
  154.               
  155.       case WM_COMMAND :
  156.               switch( LOWORD( wParam ) )
  157.               {
  158.                  case IDM_TEST :
  159.                         {
  160.                            HSTMT   hStmt;
  161.                            UCHAR   szTableName[128];
  162.                            UCHAR   szGrantee  [128];
  163.                            UCHAR   szPrivilege[128];
  164.                            UCHAR   szLBStr    [128];
  165.                            SDWORD  dwTypeLen;
  166.                            RETCODE retCode;
  167.  
  168.                            // Allocate a statement handle
  169.                            // to receive the result set.
  170.                            // ...........................
  171.                            SQLAllocStmt( hDBC, &hStmt );
  172.  
  173.                            // Call SQLTablePrivileges() to determine
  174.                            // all privileges for the department table.
  175.                            // ........................................
  176.                            retCode = SQLTablePrivileges( hStmt, 
  177.                                         NULL,         SQL_NTS, 
  178.                                         NULL,         SQL_NTS, 
  179.                                         "department", SQL_NTS ); 
  180.  
  181.                            if( retCode == SQL_SUCCESS || 
  182.                                retCode == SQL_SUCCESS_WITH_INFO )
  183.                            {
  184.                               // Bind a column to the TABLE_NAME, 
  185.                               // GRANTEE, and PRIVILEGE columns in the 
  186.                               // result set generated by SQLTablePrivileges().
  187.                               // .............................................
  188.                               SQLBindCol( hStmt, 3, SQL_C_CHAR, szTableName, 
  189.                                           sizeof( szTableName ), &dwTypeLen );
  190.  
  191.                               SQLBindCol( hStmt, 5, SQL_C_CHAR, szGrantee, 
  192.                                           sizeof( szGrantee ), &dwTypeLen );
  193.  
  194.                               SQLBindCol( hStmt, 6, SQL_C_CHAR, szPrivilege, 
  195.                                           sizeof( szPrivilege ), &dwTypeLen );
  196.  
  197.                               // Fetch all the records and
  198.                               // display them in the list box.
  199.                               //..............................
  200.                               retCode = SQLFetch( hStmt );
  201.  
  202.                               while ( retCode == SQL_SUCCESS )
  203.                               {
  204.                                  // Set string contents to be the 
  205.                                  // tablename, grantee, privilege.
  206.                                  // ..............................
  207.                                  szLBStr[0] = '\0';
  208.                                  strcat( szLBStr, szTableName ); 
  209.                                  strcat( szLBStr, "\t" );
  210.                                  strcat( szLBStr, szGrantee );
  211.                                  strcat( szLBStr, "\t" );
  212.                                  strcat( szLBStr, szPrivilege );
  213.  
  214.                                  SendMessage( hList, LB_ADDSTRING, 
  215.                                               0, (LPARAM)szLBStr );
  216.  
  217.                                  retCode = SQLFetch( hStmt );
  218.                               }
  219.                            }
  220.  
  221.                            SQLFreeStmt( hStmt, SQL_DROP );
  222.                         }
  223.                         break;
  224.  
  225.                  case IDM_ABOUT :
  226.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  227.                         break;
  228.  
  229.                  case IDM_EXIT :
  230.                         DestroyWindow( hWnd );
  231.                         break;
  232.               }
  233.               break;
  234.       
  235.       case WM_DESTROY :
  236.               PostQuitMessage(0);
  237.  
  238.               // Disconnect Environment.
  239.               //........................
  240.               SQLDisconnect( hDBC );
  241.  
  242.               // Free Connection and Environment.
  243.               //.................................
  244.               SQLFreeConnect( hDBC );
  245.               SQLFreeEnv( hEnv );
  246.               break;
  247.  
  248.       default :
  249.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  250.    }
  251.  
  252.    return( 0L );
  253. }
  254.  
  255.  
  256. LRESULT CALLBACK About( HWND hDlg,           
  257.                         UINT message,        
  258.                         WPARAM wParam,       
  259.                         LPARAM lParam)
  260. {
  261.    switch (message) 
  262.    {
  263.        case WM_INITDIALOG: 
  264.                return (TRUE);
  265.  
  266.        case WM_COMMAND:                              
  267.                if (   LOWORD(wParam) == IDOK         
  268.                    || LOWORD(wParam) == IDCANCEL)    
  269.                {
  270.                        EndDialog(hDlg, TRUE);        
  271.                        return (TRUE);
  272.                }
  273.                break;
  274.    }
  275.  
  276.    return (FALSE); 
  277. }
  278.  
  279.